﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Collections.Generic;

namespace DragAndDropRectanglesOnXAxis
{
    public partial class MainWindow : Window
    {
        private Rectangle selectedRectangle = null; // 当前选中的矩形
        private Point mouseOffset; // 鼠标与矩形左上角的偏移量
        private List<Rectangle> rectangles = new List<Rectangle>(); // 存储所有矩形的列表
        private double xOffset = 100; // 起始X位置偏移量
        private double canvasWidth = 800; // Canvas 宽度
        private double canvasHeight = 400; // Canvas 高度
        private double scaleInterval = 20; // 刻度线的间隔

        public MainWindow()
        {
            InitializeComponent();
            DrawXAxisAndSeparateTicks(); // 绘制X轴刻度线
        }

        // 绘制X轴的刻度线
        // 绘制X轴和刻度线
        // 绘制X轴和独立的刻度线
        private void DrawXAxisAndSeparateTicks()
        {
            double xAxisYPosition = canvasHeight / 2; // X轴的Y坐标位置
            double tickLength = 10; // 刻度线长度

            // 绘制X轴
            var xAxisLine = new Line
            {
                X1 = 50, // X轴起点
                Y1 = xAxisYPosition,
                X2 = canvasWidth - 50, // X轴终点
                Y2 = xAxisYPosition,
                Stroke = Brushes.Black,
                StrokeThickness = 2
            };
            DrawingCanvas.Children.Add(xAxisLine);

            // 绘制刻度线
            double xPosition = xAxisLine.X1; // 从X轴的起点开始
            while (xPosition <= xAxisLine.X2)
            {
                // 添加刻度线
                var tickLine = new Line
                {
                    X1 = xPosition,
                    Y1 = xAxisYPosition - tickLength / 2, // 刻度线的起点（位于X轴上下）
                    X2 = xPosition,
                    Y2 = xAxisYPosition + tickLength / 2,
                    Stroke = Brushes.Black,
                    StrokeThickness = 1
                };

                // 添加刻度值
                var tickValue = new TextBlock
                {
                    Text = ((int)(xPosition - xAxisLine.X1)).ToString(),
                    Foreground = Brushes.Black
                };
                Canvas.SetLeft(tickValue, xPosition - 10); // 调整刻度值的X位置
                Canvas.SetTop(tickValue, xAxisYPosition + tickLength + 5); // 调整刻度值的Y位置

                // 将刻度线和刻度值添加到画布
                DrawingCanvas.Children.Add(tickLine);
                DrawingCanvas.Children.Add(tickValue);

                xPosition += scaleInterval; // 移动到下一个刻度
            }
        }



        // 用户点击添加矩形按钮时调用
        private void AddRectangleButton_Click(object sender, RoutedEventArgs e)
        {
            // 获取用户输入的矩形宽度和高度
            if (double.TryParse(WidthInput.Text, out double width) && double.TryParse(HeightInput.Text, out double height))
            {
                AddRectangle(width, height); // 添加矩形
            }
            else
            {
                MessageBox.Show("请输入有效的矩形宽度和高度！", "输入错误", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        // 根据输入的宽度和高度生成矩形
        private void AddRectangle(double width, double height)
        {
            var rect = new Rectangle
            {
                Width = width,
                Height = height,
                Stroke = Brushes.Black,
                StrokeThickness = 2,
                Fill = Brushes.Transparent
            };

            // 计算矩形的初始位置，确保它的水平中心与X轴对齐
            double xPosition = xOffset + rectangles.Count * width;
            Canvas.SetLeft(rect, xPosition - width / 2); // 确保矩形的中心与X轴重合
            Canvas.SetTop(rect, 180); // Y轴位置保持一致

            rectangles.Add(rect);
            DrawingCanvas.Children.Add(rect);
        }

        // 鼠标按下事件：选中矩形并准备拖动
        private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point mousePos = e.GetPosition(DrawingCanvas);

            // 检查点击的矩形
            foreach (var rect in rectangles)
            {
                if (rect.IsMouseOver)
                {
                    selectedRectangle = rect;
                    mouseOffset = new Point(mousePos.X - Canvas.GetLeft(rect), mousePos.Y - Canvas.GetTop(rect));
                    selectedRectangle.CaptureMouse();
                    break;
                }
            }

        }

        // 鼠标移动事件：拖动矩形
        private void Canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (selectedRectangle != null && e.LeftButton == MouseButtonState.Pressed)
            {
                Point mousePos = e.GetPosition(DrawingCanvas);
                double newX = mousePos.X - mouseOffset.X;
                double newY = Canvas.GetTop(selectedRectangle);

                // 边界检查：确保矩形不会超出画布
                if (newX < xOffset) newX = xOffset; // 限制左边界
                if (newX + selectedRectangle.Width > canvasWidth) newX = canvasWidth - selectedRectangle.Width; // 限制右边界

                // 更新矩形位置
                Canvas.SetLeft(selectedRectangle, newX);

                // 处理矩形排序
                ReorderRectangles();
            }
        }

        // 鼠标释放事件：释放选中的矩形
        private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (selectedRectangle != null)
            {
                selectedRectangle.ReleaseMouseCapture();
                selectedRectangle = null; // 释放矩形
            }
        }

        // 矩形排序：根据矩形的X坐标重新排序
        private void ReorderRectangles()
        {
            // 获取所有矩形的X坐标并排序
            var sortedRectangles = new List<Rectangle>(rectangles);
            sortedRectangles.Sort((a, b) => Canvas.GetLeft(a).CompareTo(Canvas.GetLeft(b)));

            double xPosition = xOffset;

            // 按顺序重新排列矩形
            foreach (var rect in sortedRectangles)
            {
                Canvas.SetLeft(rect, xPosition - rect.Width / 2); // 确保矩形水平中心与X轴重合
                xPosition += rect.Width; // 更新下一个矩形的位置
            }
        }
    }
}
